package com.exercise.mddocs;

import com.github.cosycode.common.util.io.FileSystemUtils;
import com.github.cosycode.common.util.io.FileUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2022/1/7
 * </p>
 *
 * @author CPF
 * @since 1.0
 **/
@Slf4j
public class MarkdownHeadAdd {

    static volatile int number = 100000;

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("P:\\git-note\\cpfree\\devops");

        StringBuilder sb = new StringBuilder();

        FileFilter fileFilter = f -> {
            final String name = f.getName();
            if (f.isDirectory()) {
                return !name.contains(".git");
            }
            if (f.isFile()) {
                return !name.startsWith("_") && name.endsWith(".md");
            }
            return false;
        };
        FileSystemUtils.fileDisposeByRecursion(file, f -> {
            try {
                FileUtils.modifyFileContent(f, content -> {
                    if (content.startsWith("---")) {
                        return null;
                    }
                    sb.setLength(0);
                    sb.append("---\nkeys: \ntype: copy,blog,trim\nurl: <>\nid: 220100-").append(number++).append("\n---\n\n");
                    sb.append(content.trim()).append("\n");
                    log.info("添加头: {}", f.getPath());
                    return sb.toString();
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, fileFilter);
    }

}
